package com.xsq.page;

import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xsq.BaseApplication;
import com.xsq.R;
import com.xsq.data.CardInfo;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @ViewInject(R.id.text_logo)
    TextView logo;
    @ViewInject(R.id.radioGroup)
    RadioGroup radioGroup;
    @ViewInject(R.id.radioConsume)
    RadioButton radioConsume;
    @ViewInject(R.id.radioParamsSet)
    RadioButton radioParamsSet;
    @ViewInject(R.id.radioReport)
    RadioButton radioReport;
    @ViewInject(R.id.frame_layout)
    FrameLayout frameLayout;

    FragmentTransaction fragmentTransaction;
    BaseFragment mCurrentFragment;
    MainConsume mainConsumeFragment;
    MainConsumeAuto mainConsumeAutoFragment;
    MainParamSet mainParamSetFragment;
    MainReport mainReportFragment;
    final static int COUNTS = 3;// 点击次数
    final static long DURATION = 5000;// 规定有效时间
    long[] mHits = new long[COUNTS];

    //客户屏Dialog
    ShopperPage mPresentation;
    //公共需要的值
    String cardNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        x.view().inject(this);
        radioGroup.setOnCheckedChangeListener(this);
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if(BaseApplication.getInstance().getPriceType() == BaseFragment.PriceType.Auto) {
            mainConsumeAutoFragment = MainConsumeAuto.newInstance();
            fragmentTransaction.replace(R.id.frame_layout, mainConsumeAutoFragment);
        }else {
            mainConsumeFragment = MainConsume.newInstance();
            fragmentTransaction.replace(R.id.frame_layout, mainConsumeFragment);
        }
        fragmentTransaction .commitAllowingStateLoss();

        //双屏需要的权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                try {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:" + getPackageName()));
                    startActivityForResult(intent, 10);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                showShopperPage();
            }
        } else {
            showShopperPage();
        }

        radioConsume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //每次点击时，数组向前移动一位
                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
                //为数组最后一位赋值
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();
                if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
                    mHits = new long[COUNTS];//重新初始化数组
                    radioParamsSet.setVisibility(View.VISIBLE);
                }
            }
        });

        if(BaseApplication.getInstance().hasConfiged()) {
            radioParamsSet.setVisibility(View.GONE);
        }else {
            radioParamsSet.setVisibility(View.VISIBLE);
        }
    }

    private void showShopperPage() {
        //双屏显示
        DisplayManager mDisplayManager;//屏幕管理类
        Display[] displays;//屏幕数组
        mDisplayManager = (DisplayManager)MainActivity.this.getSystemService(Context.DISPLAY_SERVICE);
        displays =mDisplayManager.getDisplays(); //得到显示器数组
        mPresentation = new ShopperPage (getApplicationContext(),displays[1]);//displays[1]是副屏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        }else {
            mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
        }
        mPresentation.show();
    }

    public void notifyUserScreenOrderMoney(String orderMoney) {
        mPresentation.updateOrderMoney(orderMoney);
    }

    public void notifyUserScreenCardInfo(CardInfo cardInfo) {
        mPresentation.updateUserInfo(cardInfo);
    }

    public void notifyUserScreenDialog(String message, long time) {
        mPresentation.updateDialog(message, time);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (checkedId) {
            case R.id.radioConsume:
                hideFragment(mainParamSetFragment, fragmentTransaction);
                hideFragment(mainReportFragment, fragmentTransaction);
                if(BaseApplication.getInstance().getPriceType() == BaseFragment.PriceType.Auto) {
                    if (mainConsumeAutoFragment == null) {
                        mainConsumeAutoFragment = MainConsumeAuto.newInstance();
                        fragmentTransaction.add(R.id.frame_layout, mainConsumeAutoFragment);
                    } else {
                        // 显示当前fragment
                        mCurrentFragment = mainConsumeAutoFragment;
                        fragmentTransaction.show(mainConsumeAutoFragment);
                    }
                }else {
                    if (mainConsumeFragment == null) {
                        mainConsumeFragment = MainConsume.newInstance();
                        fragmentTransaction.add(R.id.frame_layout, mainConsumeFragment);
                    } else {
                        // 显示当前fragment
                        mCurrentFragment = mainConsumeFragment;
                        fragmentTransaction.show(mainConsumeFragment);
                    }
                }
                break;
            case R.id.radioParamsSet:
                if(BaseApplication.getInstance().getPriceType() == BaseFragment.PriceType.Auto) {
                    hideFragment(mainConsumeAutoFragment, fragmentTransaction);
                }else {
                    hideFragment(mainConsumeFragment, fragmentTransaction);
                }
                hideFragment(mainReportFragment, fragmentTransaction);
                if(mainParamSetFragment == null) {
                    mainParamSetFragment = MainParamSet.newInstance();
                    fragmentTransaction.add(R.id.frame_layout, mainParamSetFragment);
                }else {
                    // 显示当前fragment
                    mCurrentFragment = mainParamSetFragment;
                    fragmentTransaction.show(mainParamSetFragment);
                }
                break;
            case R.id.radioReport:
                if(BaseApplication.getInstance().getPriceType() == BaseFragment.PriceType.Auto) {
                    hideFragment(mainConsumeAutoFragment, fragmentTransaction);
                }else {
                    hideFragment(mainConsumeFragment, fragmentTransaction);
                }
                hideFragment(mainParamSetFragment, fragmentTransaction);
                if(mainReportFragment == null) {
                    mainReportFragment = MainReport.newInstance();
                    fragmentTransaction.add(R.id.frame_layout, mainReportFragment);
                }else {
                    // 显示当前fragment
                    mCurrentFragment = mainReportFragment;
                    fragmentTransaction.show(mainReportFragment);
                }
                break;
        }
        fragmentTransaction .commitAllowingStateLoss();
    }

    // 使用 fragmentTransaction 隐藏fragment
    private void hideFragment(Fragment fragment, FragmentTransaction ft) {
        if (fragment != null) {
            ft.hide(fragment);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10) {
            if (Settings.canDrawOverlays(this)) {
                showShopperPage();
            }
        }
    }
}
